﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VFTray.Mi
{
	class FileHelper
	{
        internal static bool WriteString(string content, string fn)
        {
            try
            {
                FileInfo fi_pub = new FileInfo(fn);
                using (FileStream fs = fi_pub.Open(FileMode.Create, FileAccess.ReadWrite))
                {
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(content);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("WriteString " + ex.Message);
                return false;
            }
            return true;
        }

        internal static string ReadString(string fn)
        {
            try
            {
                string _key = "";
                FileInfo fi_pri = new FileInfo(fn);
                using (FileStream fs = fi_pri.OpenRead())
                {
                    StreamReader sr = new StreamReader(fs);
                    _key = sr.ReadToEnd();
                    sr.Close();
                    fs.Close();
                }
                return _key;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("ReadString " + ex.Message);
                return null;
            }
        }

        internal static bool WriteByte(byte[] content, string fn)
        {
            try
            {
                FileInfo fi = new FileInfo(fn);
                using (FileStream fs = fi.Open(FileMode.Create, FileAccess.ReadWrite))
                {
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(content);
                    bw.Flush();
                    bw.Close();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("WriteByte " + ex.Message);
                return false;
            }
            return true;
        }

        internal static byte[] ReadByte(string fn)
        {
            try
            {
                byte[] content;
                FileInfo fi = new FileInfo(fn);
                using (FileStream fs = fi.OpenRead())
                {
                    BinaryReader br = new BinaryReader(fs);
                    content = br.ReadBytes((int)fs.Length);
                    br.Close();
                }
                return content;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("ReadByte " + ex.Message);
                return null;
            }
        }
    }
}
